package com.wd.live.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.wd.live.BaseConstant
import com.wd.live.tool.LocalData
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *@Author Nick.wu
 *Date 2023/8/24
 */
class PlazaViewModel : BaseViewModel() {
    private val size = 50
    private var page = 0

    val typeInfo: MutableLiveData<ArrayList<LocalData.TypeInfo>> by lazy {
        MutableLiveData<ArrayList<LocalData.TypeInfo>>()
    }

    val liveList: MutableLiveData<ArrayList<LocalData.LiveShow>> by lazy {
        MutableLiveData<ArrayList<LocalData.LiveShow>>()
    }
    fun getTypeList() {

        systemApi?.getFindLiveTypeList(BaseConstant.userId ?: 0, BaseConstant.sessionId ?: "")
            ?.let {
                it.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ bean ->
                        val element = LocalData.TypeInfo(0, "收藏", "")
                        bean.result.add(0, element)
                        typeInfo.value = bean.result
                    }, { error ->
                        Log.d("provideSystemApi", error.message.toString())
                    })
            }
    }

    fun getFindLiveList(id: Int, isRefresh: Boolean) {
        val params = HashMap<String, Any?>()
        params["liveType"] = id
        //如果是刷新，page为1，否则page自增
        params["page"] = if(isRefresh) 1 else  ++ page
        params["size"] = size
        systemApi?.getLiveList(BaseConstant.userId ?: 0, BaseConstant.sessionId ?: "", params)
            ?.let {
                it.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ bean ->
                        if(bean.status == "0000") {
                            liveList.value = bean.result
                        } else {
                            liveList.value = ArrayList()
                        }
                    }, { error ->
                        Log.d("provideSystemApi", error.message.toString())
                    })
            }
    }
}